/**
 * 
 */
package com.agrec.client;

import java.util.ArrayList;
import java.util.Iterator;

import com.agrec.client.listeners.SignInListener;
import com.agrec.client.services.UserServiceAsync;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.FlexTable.FlexCellFormatter;

/**
 * @author Alaa Gharbawi
 *
 */

public class SignInForm extends Composite {

	private ArrayList<SignInListener>    listeners    = new ArrayList<SignInListener>();
    private TextBox userName;
	private PasswordTextBox pwd;
	private UserServiceAsync userService;
	private FlexTable table;
	@SuppressWarnings("unused")
	private Button submitBtn;
	private FlexCellFormatter cellFormatter;
	private SignInHandler signInHandler;
	protected UserToken token;

    public SignInForm()
    {
    	init();
    }

    public SignInForm(UserServiceAsync userService) {
    	this.userService = userService;
    	init();
	}

    public SignInForm(UserServiceAsync userService, SignInListener signInListener) {
    	this.userService = userService;
    	addSignInListener(signInListener);
    	init();
	}

	public void init() {
    	signInHandler = new SignInHandler();
    	table = new FlexTable();
    	table.setCellSpacing(6);
    	cellFormatter = table.getFlexCellFormatter();
    	
    	table.setWidget(0, 0, new Label("User Name:"));
    	table.setWidget(0, 1, userName = new TextBox());
    	userName.setFocus(true);
        userName.addKeyUpHandler(signInHandler);
    	table.setWidget(1, 0, new Label("Password:"));
    	table.setWidget(1, 1, pwd = new PasswordTextBox());
        pwd.addKeyUpHandler(new SignInHandler());
    	table.setWidget(2, 0, submitBtn = new Button("Submit",signInHandler));
    	cellFormatter.setColSpan(2, 0, 2);
    	cellFormatter.setHorizontalAlignment(2, 0,
    	        HasHorizontalAlignment.ALIGN_CENTER);
    	
    	DecoratorPanel decPanel = new DecoratorPanel();
    	decPanel.setWidget(table);
    	
    	initWidget(decPanel);
    }
	public void addSignInListener(SignInListener listener)
    {
        listeners.add(listener);
    }

    public void removeSignInListener(SignInListener listener)
    {
        listeners.remove(listener);
    }

    public void onSignInAttempt(boolean passwordOK)
    {
        for(Iterator<SignInListener> it = listeners.iterator(); it.hasNext();)
        {
            SignInListener listener = (SignInListener) it.next();
            listener.onSignIn(passwordOK, token);
        }
    }
    
    private void signIn() {
    	userService.login(userName.getText(), pwd.getText(),
				new AsyncCallback<UserToken>() {
					

					public void onFailure(Throwable caught) {
						onSignInAttempt(false);
						
					}

					public void onSuccess(UserToken result) {
						token = result;
						if(result != null) {
							onSignInAttempt(true);
							
						}
						else 
							onSignInAttempt(false);
						
						
					}
				});
    }

    class SignInHandler implements KeyUpHandler, ClickHandler {
		public void onKeyUp(KeyUpEvent event) {
			if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
				signIn();
			}
			
		}

		public void onClick(ClickEvent event) {
			signIn();
			
		}
    }

}
